/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: 1.c
> Author:cjy
> Mail: 
> Created Time: 2024年08月05日 星期一 11时08分41秒
***************************************************/
// 通过管道进程a给进程b传送一个文件。
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc,const char* argv[])
{
	int fp = 0,fd = 0;
	char name[50] = {0};
	char buf[1024] = {0};
	int ret = 0;
	printf("你想要接收文件的名字:");
	scanf("%s",name);
	fd = open("./pipe",O_RDONLY);
	if(fd == -1){
		perror("open");
		exit(-1);
	}
	fp = open(name,O_RDWR | O_CREAT | O_APPEND, 0640);
	if(fp == -1){
		perror("open");
		close(fp);
		exit(-1);
	}
	int count = 0;
	int flag = 1;
	ret = read(fd,buf,sizeof(buf)-1);
		if(ret == -1){
		perror("read");
		close(fd);
		exit(-1);
	}
	printf("%s",buf);
        	ret = write(fp,buf,sizeof(buf));
        	if(ret == -1)
        	{
            		perror("write");
			close(fp);
            		close(fd);
            		exit(EXIT_FAILURE);
        }

	close(fd);
	close(fp);
    return 0;
}
